<template>
  <div class="householdregister">
    <el-form ref="registerForm" :model="registerForm"  class="register-form">
      <h4 class="title">住户注册系统</h4>
      <el-form-item prop="name" label="姓名">
        <el-input type="text" v-model="registerForm.name" auto-complete="off" placeholder="请输入姓名！" style="width: 90%"></el-input>
      </el-form-item>

      <el-form-item prop="sex" label="性别">
        <el-radio-group v-model="registerForm.sex">
          <el-radio class="radio" :label="1">男</el-radio>
          <el-radio class="radio" :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item prop="phone" label="电话">
        <el-input  type="text" v-model="registerForm.phone" auto-complete="off" placeholder="请输入电话！" style="width: 90%"></el-input>
      </el-form-item>

      <el-form-item prop="smscode" label="验证码">
        <el-input  type="text" v-model="registerForm.smscode" auto-complete="off" placeholder="请输入验证码！" style="width: 50%">
        </el-input>
        <el-button v-show="show" @click="getsmscode" class="getCode">获取验证码</el-button>
        <el-button v-show="!show" class="count">{{count}}s后重新获取</el-button>
        <!--<el-button type="primary" @click="getsmscode">获取验证码</el-button>-->
      </el-form-item>

      <el-form-item prop="wxnumber" label="微信">
        <el-input   type="text" v-model="registerForm.wxnumber" auto-complete="off" placeholder="请输入微信！" style="width: 90%"></el-input>
      </el-form-item>



      <el-form-item label="小区" prop="communityId"  style="width:80%;">
        <!--<el-input v-model="form.communityId" placeholder="请输入小区名" />-->
        <el-select v-model="registerForm.communityId" value-key="id" placeholder="请选择" clearable>
          <el-option v-for="item in community"
                     :label="item.name"
                     :value="item.id">
            <span style="float: left">{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋" prop="buildingId"  style="width:80%;">
        <!--<el-input v-model="form.buildingId" placeholder="请输入楼栋名" />-->
        <el-select v-model="registerForm.buildingId" value-key="id" placeholder="请选择" clearable>
          <el-option v-for="item in building"
                     :label="item.buildingname"
                     :value="item.id">
            <span style="float: left">{{ item.buildingname }}</span>
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item prop="address" label="地址">
        <el-input  type="text" v-model="registerForm.address" auto-complete="off" placeholder="请输入地址！"  style="width: 90%"></el-input>
      </el-form-item>


      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { smscode, householdregister } from "@/api/login";
import {listCommunity} from "@/api/basics/community";
import {listBuilding} from "@/api/basics/building";

export default {
  name: "Register",
  data() {
    return {

      show: true,
      count: '',
      timer: null,


      community:[],
      building:[],

      codeUrl: "",
      smscode:"",

      queryCommunity: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        manager: null,
        phone: null,
        address: null,
        status: null
      },

      queryBuilding: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        buildingname: null,
        status: null
      },


      registerForm: {
        name: "",
        sex:"",
        phone:"",
        smscode:"",
        wxnumber:"",
        communityId:null,
        buildingId:null,
        address:""
      },
      loading: false,
      captchaEnabled: true
    };
  },

  methods: {
    getsmscode(){

      //1.判断手机号不为空
      if(!this.registerForm.phone){
        alert("手机号不能为空");
        return;
      }

      smscode(this.registerForm).then(res => {
        /*this.community = response.rows;*/
        this.loading = false;
        this.$modal.msgSuccess("发送成功");

        if( this.$modal.msgSuccess){
          alert("手机验证码已经发送到您的手机，请在3分钟内使用");
          //4.1.发送成：倒计时
          const TIME_COUNT = 60;
          if (!this.timer) {
            this.count = TIME_COUNT;
            this.show = false;
            this.timer = setInterval(() => {
              if (this.count > 0 && this.count <= TIME_COUNT) {
                this.count--;
              } else {
                this.show = true;
                clearInterval(this.timer);
                this.timer = null;
              }
            }, 1000)
          }
        }else{
          //4.3.发送失败：提示，恢复按钮
          this.timer.attr("disabled",false);
          alert("发送失败:"+this.$modal.msg);
        }
      });

    },


    handleRegister() {
          householdregister(this.registerForm).then(res => {
            if( this.$modal.msgSuccess){
              alert("注册成功")
            }
          }).catch(() => {
          })
    },

    // 获取小区名字
    getCommunity() {
      this.loading = true;
      listCommunity(this.queryCommunity).then(response => {
        this.community = response.rows;
        this.loading = false;
      });
    },
    // 获取楼栋名字
    getBuilding() {
      this.loading = true;
      listBuilding(this.queryBuilding).then(response => {
        this.building = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  },
  mounted() {
    this.getBuilding();
    this.getCommunity();
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.householdregister {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 500px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
